from typing import List
from common.enum.strategy_group_enum import StrategyGroup
from common.enum.strategy_type_enum import StrategyType
from dto.strategy_info import StrategyInfoMetadata
from dto.strategy_assessment import RiskAssessment
from dto.strategy_stock_day import StrategyStockDay
from service.strategy.base_strategy import BaseStrategy


class ThreeDaysDecreaseGrowthStrategy(BaseStrategy):
    """
    连续3天涨幅逐渐减少策略，判断过去3天内股票涨幅是否逐渐减小。
    """

    def analyze(self, trade_info_list: List[StrategyStockDay]) -> RiskAssessment:
        """
        分析过去3天的涨幅是否逐日减少。
        """
        node_point = 0
        description = "涨幅无明显递减趋势"

        # 确保至少有3天的交易数据
        if len(trade_info_list) >= 3:
            is_increasing_and_decreasing = True

            # 检查过去3天的涨幅是否逐日减少，并确保每日为上涨
            for i in range(2, len(trade_info_list)):
                day_1_growth = (
                    trade_info_list[i - 2].close - trade_info_list[i - 2].open
                ) / trade_info_list[i - 2].open
                day_2_growth = (
                    trade_info_list[i - 1].close - trade_info_list[i - 1].open
                ) / trade_info_list[i - 1].open
                day_3_growth = (
                    trade_info_list[i].close - trade_info_list[i].open
                ) / trade_info_list[i].open

                # 检查每日是否为上涨，且涨幅是否递减
                if not (
                    day_1_growth > 0
                    and day_2_growth > 0
                    and day_3_growth > 0
                    and day_1_growth > day_2_growth > day_3_growth
                ):
                    is_increasing_and_decreasing = False
                    break

            if is_increasing_and_decreasing:
                node_point = 5
                description = "过去3天内股票均为上涨，且涨幅逐渐减少，符合策略条件。"

        # 创建策略信息对象
        return RiskAssessment(
            stock_code=trade_info_list[0].stock_code,
            description=description,
            config=self.strategyConfig(),
            node_point=node_point,
        )

    def strategyConfig(self) -> StrategyInfoMetadata:
        """
        返回策略的配置信息。
        """
        return StrategyInfoMetadata(
            strategy_code="three_days_decrease_growth",
            strategy_name="连续3天涨幅递减策略",
            strategy_group=StrategyGroup.RISK,
            strategy_type=StrategyType.PRICE,
            analysis_day=3,
            strategy_level=2,
        )
